package com.wangm.elasticsearch;

import com.wangm.elasticsearch.entity.User;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangmeng
 * @since 2022/1/13
 */
@RestController
@DependsOn("elasticsearchTemplate")
public class TestController {

    @Autowired
    ElasticsearchRestTemplate template;

    @GetMapping("test")
    public String test() {
        //
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery());
        SearchHits<User> search = template.search(nativeSearchQueryBuilder.build(), User.class);
        return search.toString();
    }
}
